# -*-  conding: utf-8 -*-
# Author: czh
# Datetime : 2022/5/10 13:36
import time
from pprint import pprint
import pysnowball as ball
import csv
from datetime import date, timedelta, datetime
import akshare as ak
import tushare as ts

day = date.today()
today = format(day.strftime('%Y%m%d'))

pro = ts.pro_api('cf37ab059637ac7f5d628bdd046a9bcb2c1e34c2fa7546e6b8659225')


def get_next_date(ts_code, start_date):
    """
    根据日期获取未来五个交易日的增幅
    ts_code:股票代码600313.SH ； start_date：开始时间
    """
    pct_chg_list = []
    df = pro.trade_cal(
        **{"exchange": "", "cal_date": "", "start_date": start_date, "end_date": "", "is_open": 1, "limit": "",
           "offset": 1
           }, fields=["exchange", "cal_date", "is_open", "pretrade_date"
                      ])
    for idx, data in df.iterrows():
        # 拉取数据
        pct_chg = pro.daily(
            **{"ts_code": ts_code, "trade_date": data[1], "start_date": "", "end_date": "", "offset": "",
               "limit": ""
               }, fields=[
                "pct_chg"
            ])
        # print(pct_chg)
        pct_chg_list.append(pct_chg.iloc[0, 0])
        if len(pct_chg_list) > 5:
            return pct_chg_list


def get_symbol():
    # df = ak.stock_zh_a_spot()
    # df.to_csv('./shishi.csv')
    with open("shishi.csv", "r", encoding='utf-8-sig') as f:
        f_csv = csv.reader(f)
        # 第一行不用读取
        next(f_csv)

        symbol_list = []
        """

        0,1代码,2名称,3最新价,4涨跌额,5涨跌幅,6买入,7卖出,8昨收,9今开,10最高,11最低,12成交量,13成交额
        """
        # for idx, data in df.iterrows():
        for data in f_csv:
            # print(data)
            symbol = data[1].upper()
            current = float(data[3])
            open1 = float(data[9])
            low = float(data[11])
            high = float(data[10])
            name = data[2]
            pct_chg = float(data[5])
            pre_close = float(data[8])
            """ 计算下影线 / 柱体 大于2.5 踢出ST股票 的股票代码, 涨跌幅小于0的"""
            shiti = abs(open1 - current)
            xiaying = min(open1, current) - low
            shangying = high - max(open1, current)
            chazhi = high - low
            # if shiti == 0.0 or xiaying == 0.0 or "ST" in name or pct_chg < 0:
            if xiaying == 0.0 or "ST" in name or pct_chg < 0:
                # print("跳过：", shiti, xiaying)
                pass
            else:
                # jieguo = round(xiaying / shiti, 2)
                # jieguo2 = round(shangying / shiti, 2)
                jieguo3 = round(chazhi / pre_close, 2)
                jieguo4 = round(shangying / xiaying, 2)
                # if jieguo > 2 and jieguo3 > 0.06:
                if jieguo3 > 0.06 and jieguo4 < 0.2:
                    symbol_name = symbol + name
                    symbol_list.append(symbol_name)

    return symbol_list


if __name__ == '__main__':
    # current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
    symbol_list = get_symbol()
    print(symbol_list)
    print("初步筛选数据：", len(symbol_list))
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))

    new_name_list = []
    for symbol_name in symbol_list:
        sysmbol = symbol_name[:8]
        name = symbol_name[8:]
        # print(symbol_name)
        try:
            res = ball.quotec(sysmbol)
            float_market_capital = float(res["data"][0]["float_market_capital"])
            # 流通值在 150亿以上
            if float_market_capital > 15000000000:
                new_name_list.append(name)
                # pct_chg_list = get_next_date(ts_code, trade_date)
                # print(trade_date, "当日涨幅：", pct, "    下一日涨幅：", pct_chg.iloc[0, 0])
                # print(trade_date, "当日涨幅：", pct, pct_chg_list)
        except TypeError:
            print("出现异常：", name)
    print(len(new_name_list))
    pprint(new_name_list)
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
